package com.me.videoservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.me.videoservice.model.msg.DeviceMsg;
import com.me.videoservice.service.DeviceLocationInfoService;
import com.me.videoservice.service.MqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

@Service
public class MqttDeviceMsgServiceImpl implements MqttService {

  @Autowired
  private DeviceLocationInfoService deviceLocationInfoService;

  @Override
  public Boolean topic(String topic) {
    return topic.matches("/device/gps/.*");
  }

  @Override
  public void process(Message<?> message, String topic) {
    DeviceMsg msg = JSON
        .parseObject((String) message.getPayload(), DeviceMsg.class);
    deviceLocationInfoService.save(msg);
  }

}
